package com.watson.thread;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;

/**
 * @program: mytest
 * @description: thread pool test
 * @author: zhangpeng348@jd.com
 * @date: 2018-12-06 15:41
 **/
public class ThreadPoolTest {

    public ThreadPoolTest() {

    }

    public void task() {

        BasicThreadFactory build = new BasicThreadFactory
                .Builder()
                .namingPattern("")
                .daemon(true)
                .build();

        ThreadFactory build1 = new ThreadFactoryBuilder().setNameFormat("").build();


        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(2, build);


        service.schedule(new Runnable() {
            @Override
            public void run() {
                return;
            }
        }, 10, TimeUnit.SECONDS);
        //thread pool
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                10,
                10,
                100,
                TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2), build);

        executor.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread pool executor!");
            }
        });

        executor.submit(new Callable<String>() { public String call() {return "";}});

        //schedule thread poll executor
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor =
                new ScheduledThreadPoolExecutor(10, build);
        //延迟执行一次
        scheduledThreadPoolExecutor.schedule(new Job(),1, TimeUnit.SECONDS);

        //只执行一次定时任务不需要period参数，周期执行加上period参数
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("timer task");
            }
        }, new Date(), 1000);

        //周期定时任务
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                System.out.println("timer schedule at fixed rate");
            }
        }, 1000, 1000);

    }

    public class Job implements Callable {
        @Override
        public Object call() throws Exception {
            System.out.println("scheduled thread poll executor");
            return null;
        }
    }

    public void forkPoolTest() {
        ForkJoinPool pool = ForkJoinPool.commonPool();
        SumTask sumTask = new SumTask();
        pool.submit(sumTask);
    }

    public static void main(String[] args) {
        ThreadPoolTest test = new ThreadPoolTest();
        test.task();
    }

    public class SumTask extends RecursiveTask<Integer> {

        @Override
        protected Integer compute() {
            return null;
        }
    }

    public void threadTest() {
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        ExecutorService fixedService = Executors.newFixedThreadPool(3);
        ExecutorService singleService = Executors.newSingleThreadExecutor();
        ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(2);

        try {

            for (int i = 0; i < 10; i++) {
                final int index = i;
                scheduledService.schedule(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(index * 1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        System.out.println(index+"当前线程ID"+Thread.currentThread().getId());
                        String str = null;
                        str.length();
                    }
                }, 1, TimeUnit.SECONDS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

